// components/tagSelector/tagSelector.js
var util = require('../../utils/util.js')
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    apiType: {
      type: String,
      value: '1'
    },
    showPre: {
      type: Boolean,
      value: !1
    },
    needShowMore: {
      type: Boolean,
      value: !1
    },
    buttonTitle: {
      type: String,
      value: '开始匹配'
    },
    title: {
      type: String,
      value: '添加自己性格标签'
    },
    subtitle: {
      type: String,
      value: '我是不一样的烟火'
    },
    cutsomTag: {
      type: String,
      value: '',
      observer: function (newVal, oldVal, changedPath) {
        if (newVal.trim() != oldVal.trim()) {
          const result = this.addTag(newVal)
          if (!result) {
            wx.showToast({
              title: '添加失败,请检查标签是否已存在',
              icon: 'none',
              duration: 2000,
              complete: () => {
                wx.hideToast()
              }
            })
          } else {
            wx.showToast({
              title: '添加成功',
              duration: 2000,
              complete: () => {
              }
            })
          }
        }
      }
    },
    initTagSelectedArray: {
      type: Array,
      value: []
    },
    initCustomTagSelectedArray: {
      type: Array,
      value: []
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    dataSource: [],
    custom_tag_array: [],
  },
  ready: function () {
    util.getKeyWord(this.properties.apiType == '1' ? { 'param': 'introduce' } : { 'type': 'matching' },this.getkeyWordDidResponse, this.requestDidFaild, this)
    // util.getKeyWord.call(this, this.getkeyWordDidResponse, this.getkeyWordDidResponse)

  },

  /**
   * 组件的方法列表
   */
  methods: {
    addBtnClick: function (e) {
      if (this.properties.apiType==3) {
        return
      }
      this.triggerEvent('customTagDidClick', {}, {})
    },
    subItemDidTaped: function (e) {
      var tapIndex = e.target.dataset.index
      this.data.dataSource[tapIndex].selected = !this.data.dataSource[tapIndex].selected
      this.setData({
        dataSource: this.data.dataSource
      })
    },
    customItemDidTaped: function (e) {
      var tapIndex = e.target.dataset.index, d = this.data
      this.data.custom_tag_array[tapIndex].selected = !this.data.custom_tag_array[tapIndex].selected
      this.setData({
        custom_tag_array: d.custom_tag_array
      })
    },
    nextBtnClick: function (e) {
      this.combineResult()
    },
    moreInfoBtnClick: function(e) {
      this.triggerEvent('goMoreInfo', {}, {})
    },
    goPrePage: function (e) {
      this.triggerEvent('goPre', {}, {})
    },
    addSelectProperty: function (data) {
      var array = []
       data.map(
        (e, index) => {
          e.selected = !1
          e.results.map(
            (el) => {
              el.selected = !1
              array.push(el)
            }
          )
        }
      )
      this.data.dataSource = array;
      //如果有初始化选中的标签，则先处理这些标签
      if (this.data.initTagSelectedArray.length > 0) {
        this.data.initTagSelectedArray.map(
          (id) => {
            this.data.dataSource.map(
              (e) => {
                if (e.id == id) {
                  e.selected = !0
                }
              }
            )
          }
        )
      }
      //如果有初始化的自定义标签，则先处理这些自定义的标签
      if (this.data.initCustomTagSelectedArray.length > 0) {
        this.data.custom_tag_array = []
        this.data.initCustomTagSelectedArray.map(
          (e) => {
            var obj = {}
            obj.value = e
            obj.selected = !0
            this.data.custom_tag_array.push(obj)
          }
        )
        this.setData({
          custom_tag_array: this.data.custom_tag_array,
        })
      }
      this.setData({
        dataSource: this.data.dataSource,
      })
    },
    getkeyWordDidResponse: function (res) {
      console.log('did receive response tag: ' + res)
      console.log(res.data.result)
      console.log(this)

      this.addSelectProperty(res.data.result)
    },
    addTag: function (tag) {
      console.log(tag)
      var index = this.data.custom_tag_array.indexOf(tag)
      if (index == -1) {
        var data = {}
        data.value = tag
        data.selected = !0
        this.data.custom_tag_array.push(data)
        this.setData({
          custom_tag_array: this.data.custom_tag_array,
        })
        return true
      } else {
        return false
      }
    },
    combineResult: function (e) {
      var result = []
      this.data.dataSource.map(
        (e) => {
          e.selected ? result.push(e.id) : !1
        }
      )
      if (result.length == 0) {
        wx.showToast({
          title: '请选择至少一个非自定义标签',
          icon: 'none'
        })
        return
      }
      var customResut = []
      this.data.custom_tag_array.map(
        (e) => {
          e.selected ? customResut.push(e.value) : !1
        }
      )
      console.log(customResut)
      this.triggerEvent('tagSelectedFinishedHandle', { 'tagSelectedArray': result, 'customTagSelectedArray': customResut }, {})
    }
  }
})
